package com.yukefms.rule.formula;

import com.yukefms.common.ListTools;
import com.yukefms.engine.running.RunningContext;
import com.yukefms.engine.running.MatchedStatus;
import com.yukefms.jointpoint.BodyPointCloud;
import com.yukefms.rule.formula.interfaces.AbstractStaticBAFormula;
import com.yukefms.rule.formula.interfaces.StaticBAFormula;

import java.util.ArrayList;

/**
 * @Description
 * @Author Ping
 * @Date 2021/6/21 23:16
 **/
public class StaticBAFormulaChain extends AbstractStaticBAFormula {
    private StaticBAFormula firstFormula ;
    private StaticBAFormula lastFormula ;
    public StaticBAFormulaChain() {
        this.jointTypes = new ArrayList<>() ;
    }

    public StaticBAFormulaChain(StaticBAFormula firstFormula) {
        this.firstFormula = firstFormula;
        this.lastFormula = firstFormula ;
        this.jointTypes = firstFormula.relatedJointTypes() ;
    }

    /**
     * create formula chain
     * @param formula
     * @return
     */
    public StaticBAFormulaChain add(StaticBAFormula formula) {
        if (firstFormula == null) {
            this.firstFormula = formula ;
            this.lastFormula = formula ;
            this.jointTypes = formula.relatedJointTypes() ;
        } else {
            this.lastFormula.setSuccessor(formula) ;
            this.lastFormula = formula ;
            ListTools.addAllNoDumplicate(this.jointTypes, formula.relatedJointTypes()) ;
        }
        return this ;
    }
    @Override
    public MatchedStatus matched(BodyPointCloud bodyPointCloud, RunningContext context) {
        return this.firstFormula.matched(bodyPointCloud, context) ;
    }

}
